<?php
define('_PS_ADMIN_DIR_', getcwd());
include(_PS_ADMIN_DIR_.'/../config/config.inc.php');
/* Getting cookie or logout */
require_once(dirname(__FILE__).'/init.php');

$query = Tools::getValue('q', false);
if (!$query OR $query == '' OR strlen($query) < 1)
	die();
    
if($pos = strpos($query, ' (cms:'))
	$query = substr($query, 0, $pos);

$id_cms = Tools::getValue('id_cms', false);
$excludeIds = Tools::getValue('excludeIds', false);
if ($excludeIds && $excludeIds != 'NaN')
	$excludeIds = implode(',', array_map('intval', explode(',', $excludeIds)));
else
	$excludeIds = '';

$sql = 'SELECT st.`id_style`, st.`selector`, st.style, CONCAT(c.`code`, \' \', c.`version`) AS cms
		FROM `'._DB_PREFIX_.'pt_default_style` st 
        LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (st.`id_cms` = c.`id_cms`)
		WHERE (st.`selector` LIKE \'%'.pSQL($query).'%\')'.
		(!empty($id_cms) ? ' AND st.`id_cms` = '. (int)$id_cms.' ' : ' ');
$items = Db::getInstance()->executeS($sql);

if ($items)
	foreach ($items AS $item)
		echo trim($item['selector']). ' (cms: '.$item['cms'].')' .'|'.(int)($item['id_style']).'|'. $item['style'] ."\n";

